<?php
namespace app\common\taglib\ht;
use app\common\model\Archives as ArcModel;
use app\common\model\Arctype;
use think\facade\Db;

class tagLists
{

    // 获取Article列表
    public function getLists($type,$field, $map, $cateid, $order, $day, $cache, $pagenum, $minscore, $maxscore, $ext, $usertype, $uid, $own, $how, $search, $similar, $status, $exceptid,$choice,$top)
    {
        $tag = [
            'type' => $type,
            'field' => $field,
            'map' => $map,
            'cateid' => $cateid,
            'order' => $order,
            'day' => intval($day),
            'cache' => intval($cache),
            'pagenum' => intval($pagenum),
            'minscore' => $minscore,
            'maxscore' => $maxscore,
            'ext' => $ext,
            'usertype' => $usertype,
            'uid' => intval($uid),
            'own' => intval($own),
            'how' => $how,
            'search' => $search,
            'similar' => $similar,
            'status' => $status,
            'exceptid' => $exceptid,
            'choice' => $choice,
            'top' => $top,
        ];

        /*
            * how表示选择什么类型的文档，如down表示下载数最多
            * rate评分排行,wytj表示网友推荐的,wysc网友收藏
            *
            * search如果有值，表示按照关键字去查询
            * similar如果有值，说明查找该id的相关文档
            *
            */

        $doc_mod = Db::name('doc_con');
        $select = $doc_mod->field($tag['field']); //字段
        $where[] = array('del_status', '=', 1);//未删除
        $where[] = array('convert_status', '=', 2);//已转换
//        $where[] = array('status', '=', 1);//已审核
        //条件
        if ($tag['exceptid']) {
            $where[] = array('id', '<>', $tag['exceptid']);
        }
        if ($tag['ext']) {
            $where[] = array('ext', 'in', $tag['ext']);
        }

        if ($tag['uid']) {
            if ($tag['own']) {
                $where[] = array('uid', '=', $tag['uid']);
            } elseif ($tag['usertype']) {
                switch ($tag['usertype']) {
                    case 'down':
                        $idarr = useritemid($tag['uid'], 1,1);
                        break;
                    case 'wysc':
                        $idarr = useritemid($tag['uid'], 1,2);
                        break;
                    default:
                        break;
                }
                $where[] = array('id', 'in', $idarr);
            }
        }

        $now = time();
        if ($tag['day']) {//如果填写了天数，表示查找该范围内的文档
            $where[] = array('create_time', '>=', $now - $tag['day'] * 24 * 3600);
        } else {
            $where[] = array('create_time', '<=', $now - $tag['cache']);
        }
        if ($tag['cateid']) {//如果填写了，表示查找该分类范围内的文档，可以为1,2,3,4用半角逗号隔开
            $where[] = array('cid', 'in', $tag['cateid']);
        }
        if ($tag['status']) {
            $where[] = array('status', '=', $tag['status']);
        }

        if ($tag['choice']) {
            $where[] = array('choice', '=', $tag['choice']);
        }
        if ($tag['top']) {
            $where[] = array('top', '=', $tag['top']);
        }

        if ($tag['minscore'] != 'min') {
            $where[] = array('score', '>=', $tag['minscore']);
            if ($tag['maxscore'] != 'max') {
                if ($tag['maxscore'] > $tag['minscore']) {
                    $where[] = array('score', 'between', array($tag['minscore'], $tag['maxscore']));
                }
            }
        } else {
            if ($tag['maxscore'] != 'max') {
                $where[] = array('score', '<=', $tag['maxscore']);
            }
        }

        if ($tag['search']) {
            $searcharr = explode(',', $tag['search']);

//        delete_empty($searcharr);
            foreach ($searcharr as $keysear => $valuesear) {

//                $searcharr[$keysear] = '%' . $valuesear . '%';
                $where[] = array('tags|title', 'like', '%' . $valuesear . '%');

            }
        }
        if ($tag['similar']) {
            $tags = Db::name('doc_con')->where('id',$tag['similar'])->value('tags');
            $tagarr = explode(',', $tags);
            foreach ($tagarr as $key => $value) {
//                $tagarr[$key] = '%' . $value . '%';
                $where[] = array('tags|title', 'like','%' . $value . '%');
            }
        }

        if ($tag['pagenum']) {

            $count = $select->where($where)->count();// 查询满足要求的总记录数
            $Page = new \think\Page($count, $tag['pagenum']);// 实例化分页类 传入总记录数和每页显示的记录数
            $data['page'] = $Page->show();// 分页显示输出
            $data['total'] = $count;
            $aa = $select->where($where)->order($tag['order'])->limit($Page->firstRow, $Page->listRows)->fetchSql(true)->select();
//        var_dump($aa); //exit;
        } else {
            $select->where($where);
//        $tag['pagenum'] && $select->limit($tag['pagenum']); //每页的文档数量
            $select->order($tag['order']); //排序
        }
        $data['list'] = $select->select()->toArray();

        foreach ($data['list'] as $key => $value) {
            $data['list'][$key]['downnum'] = downcount($value['id'], 1);
            $data['list'][$key]['wyscnum'] = wysccount($value['id'], 1);
            $data['list'][$key]['wytjnum'] = wytjcount($value['id'], 1);
        }
        if ($tag['how'] != '') {
            switch ($tag['how']) {
                case 'down':
                    foreach ($data['list'] as $key => $value) {
                        $data['list'][$key]['comcount'] = downcount($value['id'], 1);
                    }
                    usort($data['list'], 'cmpcomcount');
                    break;
                case 'rate':
                    foreach ($data['list'] as $key => $value) {
                        $data['list'][$key]['comcount'] = getraty($value['id'], 1);
                    }
                    usort($data['list'], 'cmpcomcount');
                    break;
                case 'wytj':
                    foreach ($data['list'] as $key => $value) {
                        $data['list'][$key]['comcount'] = wytjcount($value['id'], 1);
                    }
                    usort($data['list'], 'cmpcomcount');
                    break;
                case 'wysc':
                    foreach ($data['list'] as $key => $value) {
                        $data['list'][$key]['comcount'] = wysccount($value['id'], 1);
                    }
                    usort($data['list'], 'cmpcomcount');
                    break;
                case 'comment':
                    foreach ($data['list'] as $key => $value) {
                        $data['list'][$key]['comcount'] = comcount($value['id'], 1);
                    }
                    usort($data['list'], 'cmpcomcount');
                    break;
                default:
                    break;
            }
        }
//    dump($data);
        return $data;
    }
}


